import moment from 'moment';
const FORMATDAY = 'YYYY-MM-DD';
import _ from 'lodash';
/*获取选中的日期的月份，第几周，年份的开始日期、结束日期
@params date 
@params type=> month, week, year
return {startDate, endDate}
*/
const MONMENT_SPIT = {
  week: 'isoWeek',
  month: 'month',
  year: 'year',
};

const PAGE_CONTENT = {
  week: ['security', 'operate', 'transaction', 'electricitySales'],
  month: ['security', 'management', 'operate', 'transaction', 'electricitySales'],
  year: ['security', 'management', 'operate', 'transaction', 'electricitySales'],
};

const PAGE_CONTENT_KEY = {
  security: 'S',
  management: 'M',
  operate: 'O',
  transaction: 'T',
  electricitySales: 'SE',
};

const convertTreeToArray = (data, treeMap = [], depth = 0) => {
  let lintDepth = depth;
  if (!(data && data.length)) return [];
  lintDepth++;
  return data.reduce((acc, cur) => {
    cur.depth = lintDepth;
    acc.push(cur);
    if (cur.children && cur.children.length) {
      convertTreeToArray(cur.children, treeMap, lintDepth);
    }
    if (cur.secondMenuList && cur.secondMenuList.length) {
      convertTreeToArray(cur.secondMenuList, treeMap, lintDepth);
    }
    return acc;
  }, treeMap);
};

export const getPageConent = (array = [], type = 'week') => {
  let arr = [];
  const config = PAGE_CONTENT[type];
  if (config) {
    arr = array.filter((n) => config.includes(n.root ? n.root.menuCode : '-1'));
  }
  return { pageArray: arr, menuArray: config || [], PAGE_CONTENT_KEY };
};

export const getDisabledDate = (type = 'week') => {
  return moment()
    .add('days', -1)
    .endOf(MONMENT_SPIT[type] || 'month');
};

export const getStartAndEndDate = (date = moment(), type = 'week') => {
  let startDate = moment(date),
    endDate = moment(date);
  const configMoment = MONMENT_SPIT[type];
  if (configMoment) {
    startDate = startDate.startOf(configMoment);
    endDate = endDate.endOf(configMoment);
  }
  startDate = startDate.format(FORMATDAY);
  endDate = endDate.format(FORMATDAY);
  return { startDate, endDate };
};

const appIds = (treeData, arr) => {
  return treeData.map((n) => {
    let child = appIds(n.children, [...arr, n.deptId]);
    return { ...n, link: [...arr, n.deptId], children: child };
  });
};

export const filterTreeDataByIds = (treeData, keys = []) => {
  const flatArray = convertTreeToArray(appIds(treeData, []));
  const checkArray = flatArray.filter((n) => keys.includes(n.deptNum));
  const allDepts = _.union(_.flatten(checkArray.map((n) => n.link)));
  const filterArray = flatArray.filter((n) => allDepts.includes(n.deptId));
  return handleLastTree(treeData, filterArray);
};

/**
 * 处理最后的tree
 * @method handleLastTree
 * @param {Array} treeData 树
 * @param  {Array} filterArray 选中的集合
 * @return {Array} 终极tree
 */
const handleLastTree = (treeData, filterArray) => {
  let tree = JSON.parse(JSON.stringify(treeData));
  let data = [];
  tree.forEach((item) => {
    let arr = filterArray.filter((n) => n.deptNum === item.deptNum);
    if (arr.length) {
      if (item.children) {
        item.children = handleLastTree(item.children, filterArray);
      }
      data.push(item);
    }
  });
  return data;
};

/*根据选中的keys集合，过滤treeData数据
 @params treeData 
 @keys  选中的keys集合
 return newTreeData[]
 */
export const getTreeDataByIds = (treeData, keys) => {
  let newTreeData = [];
  treeData.map((item) => {
    if (item.children) {
      let newNodes = getTreeDataByIds(item.children, keys);
      if (newNodes.length) {
        item.children = newNodes;
        newTreeData.push(item);
      }
    } else if (keys.includes(item.deptNum)) {
      newTreeData.push(item);
    }
  });

  return newTreeData;
};
